<template>
  <div>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="margin-left: 20px;">
        <el-form ref="queryForm" size="small" :inline="true">
        <el-form-item label="关卡" prop="idiomAdventureValue">
          <el-input-number v-model="selectForm.id" controls-position="right" :min="1" ></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="selectLevel()"
            >搜索</el-button
          >
        </el-form-item>
    </el-form>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="margin-left: 20px;">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="insertLevel()"
          >新建关卡</el-button
        >
      </el-col>
    </el-row>
    <!-- <el-button @click="openInsertLevel()" type="primary" plain style="margin-left:10px">新增关卡</el-button> -->
    <el-table :data="levelData" style="width: 100%; margin-top: 10px; margin-left: 15px" @selection-change="handleSelectionChange" :header-cell-class-name="'headerBg'">
      <el-table-column type="selection" width="55" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="level" label="关卡" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="idiomValue" label="成语" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" width="350px" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="primary" size="small" style="background:none;border:0px"><span style="color:#409EFF;" @click="openXiangxi(scope.row)"><i class="el-icon-share"></i>关卡详情</span></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination :page-sizes="[5, 10, 15, 20]" :page-size="pages.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pages.total" background :current-page.sync="pages.pageNum"
      @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectForm:{},
      levelData: [],
      pages: {
        // 当前页
        pageNum: 1,
        // 每页几条数据
        pageSize: 5,
        total: 0,
      },
      addButton: false,
      upButton: false,
      form: {},
    };
  },
  mounted() {
    this.selectLevel();
  },
  methods: {
    handleInput(event) {
      const input = event.target.value.trim();
      // 使用正则表达式匹配是否为纯数字
      const regex = /^\d+$/;
      if (!regex.test(input)) {
        // 如果输入不是纯数字，则清空输入框
        this.selectForm.id = '';
        this.$message.warning("仅限输入数字!")
      }
    },
    // 打开添加
    openInsertLevel() {
      this.addButton = true;
    },
    openUp(row) {
        this.upButton = true;
        const aa = JSON.stringify(row);
        this.form = JSON.parse(aa);

    },
    updateLevel() {
      this.$axios
        .post("/JieLongIdiomLevel/updateLevel", this.form)
        .then((res) => {
          if (res.data.code === 200) {
            this.upButton = false;
            this.form = {};
            this.selectLevel();
            this.$message.success("修改成功");
          } else {
            this.$message.success("修改失败");
          }
        });
    },
    // 添加关卡
    insertLevel() {
      this.$axios
        .post("/JieLongIdiomLevel/insertLevel",this.form)
        .then((res) => {
          if (res.data.code === 200) {
            this.selectLevel();
            this.$message.success("添加成功");
          } else {
            this.$message.success("添加失败");
          }
        });
    },
    // 跳转至关卡详情页
    openXiangxi(row) {
      const form = JSON.stringify(row);
      // 跳转并传递参数
      this.$router.push({ path: `/JieLongLevelInfo/${form}` });
    },
    selectLevel() {
      this.$axios
        .get("/JieLongIdiomLevel/selectLevel", {
          params: {
            pageNum: this.pages.pageNum,
            pageSize: this.pages.pageSize,
            id:this.selectForm.id
          },
        })
        .then((res) => {
          if (res.data.code === 200) {
            this.levelData = res.data.data.content;
            this.pages.total = res.data.data.totalSize;
          }
        });
    },
    //关闭对话框
    handleClose1(done) {
      // 在这里编写您的关闭逻辑
      // 例如执行某个操作后再关闭对话框
      this.form = {};
      // 调用 done() 方法来关闭对话框
      this.addButton = false;
    },
    //关闭对话框
    handleClose2(done) {
      // 在这里编写您的关闭逻辑
      // 例如执行某个操作后再关闭对话框
      this.form = {};
      // 调用 done() 方法来关闭对话框
      this.upButton = false;
    },
    handleSelectionChange(val) {
      this.ids = val.map((v) => v.id);
    },
    // 分页
    handleSizeChange(val) {
      this.pages.pageSize = val;
      this.selectLevel();
    },
    handleCurrentChange(val) {
      this.pages.pageNum = val;
      this.selectLevel();
    },
  },
};
</script>

<style>
.headerBg {
  background: #f8f8f9 !important;
}
</style>